package com.lx.algorithm.mergeSort;


/**
 * Description: 归并排序：求右边有多少数比当前数小(逆序对)
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-09-03 18:40:44
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-09-03     张李鑫                     1.0         1.0 Version
 */
public class ReversePair {


    public static int ReversePair1(int[] arr) {
        if (arr == null || arr.length < 2) {
            return 0;
        }
        return process(arr, 0, arr.length - 1);
    }

    public static int process(int[] arr, int left, int right) {
        if (left == right) {
            return 0;
        }
        int mid = left + ((right - left) >> 1);
        System.out.println(arr.length);
        return process(arr, left, mid) + process(arr, mid + 1, right) + ReversePair(arr, left, mid, right);
    }


    public static int ReversePair(int[] arr) {
        int count = 0;
        int mergeSize = 1;
        int arrLength = arr.length;
        while (mergeSize < arrLength) {
            int left = 0;
            while (left < arrLength) {
                int mid = left + mergeSize - 1;
                if (mid > arrLength) {
                    break;
                }
                int right = mid + Math.min(mergeSize, arrLength - mid - 1);
                count += ReversePair(arr, left, mid, right);
                //很关键 不能+步长 因为会存在越界的情况 一定要从上一组右组数结束的索引+1
                left = right + 1;
            }
            if (mergeSize > arrLength / 2) {
                break;
            }
            mergeSize <<= 1;
        }

        return count;
    }


    /**
     * <p>
     * 归并排序：求右边有多少数比当前数小(逆序对)
     * 从左右组最后的索引开始比较
     * 如果右组数大于左组数count++;
     * </p>
     *
     * @param arr
     * @param left
     * @param mid
     * @param right
     * @return int
     * @Author zlx
     * @date 2021/9/5
     */
    public static int ReversePair(int[] arr, int left, int mid, int right) {

        int[] mergeArr = new int[right - left + 1];
        int mergeIndex = mergeArr.length - 1;
        int p1 = mid;
        int p2 = right;
        int count = 0;
        while (p1 >= left && p2 > mid) {
            count += arr[p1] > arr[p2] ? (p2 - mid) : 0;
            mergeArr[mergeIndex--] = arr[p1] > arr[p2] ? arr[p1--] : arr[p2--];
        }
        while (p1 >= left) {
            mergeArr[mergeIndex--] = arr[p1--];
        }
        while (p2 > mid ) {
            mergeArr[mergeIndex--] = arr[p2--];
        }
        for (int i = 0; i < mergeArr.length; i++) {
            arr[left + i] = mergeArr[i];
        }
        return count;
    }

    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100;
        System.out.println("测试开始");
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            if (ReversePair1(arr1) != comparator(arr2)) {
                System.out.println("Oops!");
                printArray(arr1);
                printArray(arr2);
                break;
            }
        }
        System.out.println("测试结束");

    }


    // for test
    public static int comparator(int[] arr) {
        int ans = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    ans++;
                }
            }
        }
        return ans;
    }

    // for test
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
        }
        return arr;
    }


    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    // for test
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    // for test
    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }


}
